package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.FollowListBean;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class FollowRecordVM extends TitleVM {
    public FollowRecordVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("房源跟进记录");
    }

    public ObservableField<String> state = new ObservableField<>("0");


    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    public ObservableList<DGJItemVM> dgjItemVMObservableList = new ObservableArrayList<>();
    public ItemBinding<DGJItemVM> dgjItemVMItemBinding = ItemBinding.of(BR.viewModel, R.layout.item_follow_dgj);

    public ObservableField<Integer> showDgj = new ObservableField<>(View.VISIBLE);
    public ObservableField<Integer> showGjz = new ObservableField<>(View.GONE);


    public ObservableList<GJZItemVM> gjzItemVMObservableList = new ObservableArrayList<>();
    public ItemBinding<GJZItemVM> gjzItemVMItemBinding = ItemBinding.of(BR.viewModel, R.layout.item_follow_gjz);

    private void getData() {
        model.followup(AppConfig.getCurMap(model))
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<FollowListBean>() {
                    @Override
                    public void onNext(FollowListBean o) {
                        if (o.issucc) {
                            if (state.get().equals("0")) {
                                showDgj.set(View.VISIBLE);
                                showGjz.set(View.GONE);
                                dgjItemVMObservableList.clear();
                                for (FollowListBean.DataDTO.TobeFollowDTO dgjBean : o.getData().getTobeFollow()) {
                                    DGJItemVM itemVM = new DGJItemVM(titleVM);
                                    itemVM.setData(dgjBean);
                                    dgjItemVMObservableList.add(itemVM);
                                }
                            } else {
                                showDgj.set(View.GONE);
                                showGjz.set(View.VISIBLE);
                                gjzItemVMObservableList.clear();
                                for (FollowListBean.DataDTO.FollowingUpDTO gjzBean : o.getData().getFollowingUp()) {
                                    GJZItemVM itemVM = new GJZItemVM(titleVM);
                                    itemVM.setData(gjzBean);
                                    gjzItemVMObservableList.add(itemVM);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
